package com.shzstr.diandiantaojin.wxapi;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.baibei.crossboarder.R;
import com.baibei.module.BuildConfig;
import com.blankj.utilcode.utils.LogUtils;
import com.blankj.utilcode.utils.ToastUtils;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

public class WXPayEntryActivity extends AppCompatActivity implements IWXAPIEventHandler{

    private static final String APP_ID = BuildConfig.WECHAT_APP_ID;
    private IWXAPI api;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, APP_ID);
        api.handleIntent(getIntent(),this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent,this);
    }

    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp resp) {
//        Log.i("PayResult",baseResp.errCode+"");
        Log.i("Luke = ",resp.toString());

        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            int code = resp.errCode;
            switch (code) {
                case 0:
                    ToastUtils.showLongToast("支付成功");
                    finish();
                    break;
                case -1:
                    ToastUtils.showLongToast("支付失败");
                    finish();
                    break;
                case -2:
                    ToastUtils.showLongToast("支付取消");
                    finish();
                    break;
                default:
                    ToastUtils.showLongToast("支付失败");
                    setResult(RESULT_OK);
                    finish();
                    break;
            }
        }
    }
}
